﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace ClassLibrary {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            // Create dialog.
            ProgressDialog dlgProgress = new ProgressDialog(true, LongOperation);
            //ProgressDialog dlgProgress = new ProgressDialog();

            // Show dialog with Synchronous/blocking call.
            // LongOperation() is called by dialog.
            dlgProgress.ShowDialog(); // Synchronous/blocking call.
        }


        private void LongOperation(object sender, DoWorkEventArgs e) {
            BackgroundWorker worker = sender as BackgroundWorker;

            int max = 20;
            for (int i = 0; i < max; i++) {
                // See if cacel button was pressed.
                if (worker.CancellationPending) {
                    // Simulate time for clean-up.
                    Thread.Sleep(2000);
                    break;
                }

                int percent = i * 100 / max;

                // render a string to display on the progress dialog.
                string userState = percent.ToString();

                // Append to string just to show multi-line user-status info.
                //if (percent >= 45 && percent <= 55) { userState += "Half way"; }
                //if (percent >= 85) { userState += "Almost done"; }

                // Report percent and user-status info to dialog.
                worker.ReportProgress(percent, userState);

                // Simulate time-consuming operation
                Thread.Sleep(800);
            }
        }

    }
}
